import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-08-03
 * Time:9:24
 */

class CallableTest{

    public void test(){



    }


}


public class JUCcountDown {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService service= Executors.newFixedThreadPool(4);
        CountDownLatch latch=new CountDownLatch(20);

        for (int i = 0; i <20 ; i++) {
            int id=i;
            service.submit(()->{
                System.out.println("执行任务："+id);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("任务："+id+"执行完毕");
                latch.countDown();//计数器-1
            });
        }

        //主线程进入睡眠，计数器为0才会苏醒
        latch.await();

        System.out.println("所有任务执行完毕！");
    }
}
